package com.yjhb.android.sbz.mvp;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.http.UploadTool;
import com.jojo.android.zxlib.base.mvp.BaseView;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 创建者 zx
 * 创建时间 2020/11/17 11:13
 */
public abstract class ConventionPresenter<V extends BaseView> extends BasicsPresenter<V, ServiceApi> {
    private final String TAG = "ConventionPresenter";

    public ConventionPresenter(V v) {
        super(v);
    }

    @Override
    protected Class<ServiceApi> preModel() {
        return ServiceApi.class;
    }

    /**
     * 没有登录则返回true,并发送信号
     *
     * @return
     */
    protected boolean checkUnLogin() {
        if (App.userInfo == null) {
            if (view != null) {
                view.onFailure(-10086, "请先登录");
            }
            return true;
        }
        return false;
    }


    /**
     * 通用的上传
     *
     * @param path
     */
    public void uploadFile(final String path) {
        if (checkUnLogin()) return;
        LocalMedia localMedia = new LocalMedia();
        localMedia.setPath(path);
        localMedia.setCompressPath(path);
        ArrayList<LocalMedia> localMedia1 = new ArrayList<>();
        localMedia1.add(localMedia);
        UploadTool.OSSUploadImages(localMedia1, false, new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
//                ZxLogTool.i(data);
                uploadResult(s);
            }

            @Override
            public void onError(Throwable e) {
                uploadResult(null);
            }

            @Override
            public void onComplete() {

            }
        });

//        File file = new File(path);
//        RequestBody body = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
//        HttpTool.request(model.getUploadAvatarUrl(App.userInfo.getId(),
//                MultipartBody.Part.createFormData("file", file.getName(), body)), new HttpObserver<String>() {
//            @Override
//            public void onResult(String data) {
//                ZxLogTool.i(data);
//                uploadResult(data);
//            }
//
//            @Override
//            public void onError(int code, String msg) {
//                ZxLogTool.i(msg);
//            }
//        });
    }


    public void uploadResult(String result) {

    }

}
